#!/usr/bin/env python
from setup_utils import *
import os

# ids.storage_file

actions, options, arg = getActions()
props = actions.getGlassfish("ids.storage_file-setup.properties", ["glassfish", "port"])

prop_lists = {}
prop_lists["ids.storage_file.main.properties"] = ["dir"]
prop_lists["ids.storage_file.archive.properties"] = ["dir"]


if arg in ["CONFIGURE", "INSTALL"]:
    for prop_name, prop_list in prop_lists.items():
        actions.configure(prop_name, prop_list)
    actions.checkNoErrors()

if arg == "INSTALL":
        
    for prop_name, prop_list in prop_lists.items():
        props = actions.getProperties(prop_name, prop_list)
        if not actions.configFileExists(props.get("dir")):
            abort("Please create directory " + props.get("dir") + " relative to the config directory as specified in " + prop_name)
    
    for prop_name, prop_list in prop_lists.items():
        actions.getProperties(prop_name, prop_list)
        actions.installFile(prop_name)
   
    try:     
        app = actions.getAppName("ids.server")
        if app: actions.disableApp(app)   
        actions.removeFromApplibs("ids.storage_file*.jar")    
        actions.installToApplibs("ids.storage_file*.jar")
        actions.stopDomain()
        actions.startDomain()
        if app: actions.enableApp(app) 
    except Exception, e:
        raise
        abort(str(e))
                
if arg == "UNINSTALL":
    for prop_name in prop_lists:
        actions.removeFile(prop_name)
    
    try:
        actions.removeFromApplibs("ids.storage_file*.jar")        
    except Exception, e:
        abort(str(e))       
    
            
    
